package com.tssm.action;

import java.io.File;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.tssm.core.util.MD5;
import com.tssm.core.util.Picfiles;
import com.tssm.pojo.SarcSupplier;
import com.tssm.service.SarcSupplierService;

@Controller("userregisterAction")
@Scope("prototype")
public class UserRegisterAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	@Resource
	private SarcSupplierService sarcsupplierService;

	private String username;

	private String password;

	private String repassword;

	private String email;

	private String realName;

	private String usertype;

	private String corpName;

	private String jgcode;

	private String mobile;

	private String zone;

	private String extTel;

	private String tel;

	private String fax;

	private String province;

	private String city;

	private File business_cert;

	private String business_certFileName;

	private String cargo;

	private String rand;

	private SarcSupplier supplier;
	
	private String message;

	private MD5 md5 = new MD5();

	public String register() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String random = (String) session.getAttribute("random"); // 从session里获取验证码
		if (!rand.toLowerCase().equals(random)) {
			message="验证码输入错误！";
		} else {
			boolean flag = uservalation();
			if (flag) {
				//TsSessionUtil.refurbishUser(response, supplier);
				return "regsuccess";
			}
		}
		return "register";
	}

	public boolean uservalation() {
		if (username != null && !("").equals(username)) {
			if (password != null && !("").equals(password)
					&& repassword != null && !("").equals(repassword)) {
				if (password.equals(repassword)) {
					supplier = sarcsupplierService.findUserIsExist(username
							.trim());
					if (supplier == null) {
						boolean flag = saveuser();
						return flag;
					}
				}
			}
		}
		return false;
	}

	public boolean saveuser() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Date date = new Date();
		SarcSupplier sarcsupply = new SarcSupplier();
		Byte linshi = 0;
		sarcsupply.setAccountState(linshi); 
		sarcsupply.setAccount(username);
		sarcsupply.setPassword(md5.calcMD5(password));
		sarcsupply.setEmail(email);
		sarcsupply.setName(realName);
		sarcsupply.setSupplierName(corpName);
		sarcsupply.setOrgCode(jgcode);
		sarcsupply.setProvince(province);
		sarcsupply.setCity(city);
		sarcsupply.setTel(tel);
		sarcsupply.setSuppliserCargo(cargo);
		sarcsupply.setCreatTime(date);

		sarcsupply.setIp(request.getRemoteAddr());
		if (zone == null) {
			zone = "";
		}
		String telephone = zone + "-" + tel;
		if (extTel != null && !("").equals(extTel)) {
			telephone += "-" + extTel;
		}
		sarcsupply.setFax(telephone);
		sarcsupply.setTel(mobile);
		Byte accountstate = 0;
		sarcsupply.setAccountState(accountstate);
		Byte auditstate = 0;
		sarcsupply.setAuditState(auditstate);
		sarcsupply.setRegisterFrom("0");
		sarcsupply.setLevel("3");
		Byte ifVerif = 0;
		sarcsupply.setIfVerif(ifVerif);
		Byte ifInvestigation = 0;
		sarcsupply.setIfInvestigation(ifInvestigation);
		Byte ifQuotation = 0;
		sarcsupply.setIfQuotation(ifQuotation);
		String businessCert = "";
		if (!("").equals(business_certFileName)) {
			businessCert = Picfiles.getsrcpic(request, business_certFileName,
					business_cert, "/upload/cert/", 1000);
			sarcsupply.setBusinessCert(businessCert);
		}
		
		sarcsupply.setDr(0L);
		 boolean flag=sarcsupplierService.saveOrUpdate(sarcsupply);
		 if (flag){
			 supplier= sarcsupplierService.findUserIsExist(sarcsupply.getAccount());
		 }
		return flag;
		
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getUsertype() {
		return usertype;
	}

	public void setUsertype(String usertype) {
		this.usertype = usertype;
	}

	public String getCorpName() {
		return corpName;
	}

	public void setCorpName(String corpName) {
		this.corpName = corpName;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getZone() {
		return zone;
	}

	public void setZone(String zone) {
		this.zone = zone;
	}

	public String getExtTel() {
		return extTel;
	}

	public void setExtTel(String extTel) {
		this.extTel = extTel;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getRand() {
		return rand;
	}

	public void setRand(String rand) {
		this.rand = rand;
	}

	public SarcSupplier getSupplier() {
		return supplier;
	}

	public void setSupplier(SarcSupplier supplier) {
		this.supplier = supplier;
	}

	public MD5 getMd5() {
		return md5;
	}

	public void setMd5(MD5 md5) {
		this.md5 = md5;
	}

	public File getBusiness_cert() {
		return business_cert;
	}

	public void setBusiness_cert(File business_cert) {
		this.business_cert = business_cert;
	}

	public String getBusiness_certFileName() {
		return business_certFileName;
	}

	public void setBusiness_certFileName(String business_certFileName) {
		this.business_certFileName = business_certFileName;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public String getJgcode() {
		return jgcode;
	}

	public void setJgcode(String jgcode) {
		this.jgcode = jgcode;
	}

	public String getCargo() {
		return cargo;
	}

	public void setCargo(String cargo) {
		this.cargo = cargo;
	}

}
